#!C:\python25\python.exe
# -*- coding: GB18030 -*-

from xml.sax import parse, SAXParseException, ContentHandler
from xmlData import *
from string import *

class CParserHandler( ContentHandler ):
    def __init__( self, CAllDataDetail ):
        ContentHandler.__init__( self )

        self.strStartTagName = ''
        self.bDataFg = 0
        self.strDataName = ' '
        self.cAllData = CAllDataDetail
        self.dict = CSingleData()

    def startElement( self, name, attributes ):
        if lower(name) == "head":
            self.strStartTagName = "head"
        elif lower(name) == "box" :
            self.strStartTagName = "box"
        elif lower(name) == "detail" :
            self.strStartTagName = "detail"
        elif self.strStartTagName == "detail" :
            if lower(name) == "data" :
                self.dict.clear()
                self.bDataFg = 1
                for attrName in attributes.getNames():
                    if attrName == "group":
                        self.dict.setValue("group", int(attributes.getValue(attrName)))
            else :
                if self.bDataFg == 1 :
                    self.strDataName = lower(name)
        elif lower(name) == "totalsum" :
            self.strStartTagName = "totalsum"
        elif lower(name) == "tail" :
            self.strStartTagName = "tail"

    def endElement( self, name ):
        if self.strStartTagName == "detail" :
            if self.bDataFg and lower(name) == "data":
                self.cAllData.addData(self.dict.getValue("variable"), self.dict)
                self.bDataFg = 0
                #self.cAllData.printValue()
                self.dict.clear()

        self.strDataName = ' '

        if (lower(name) == "detail") :
            self.strStartTagName = ' '

    def characters(self, contents):
        if self.strStartTagName == "detail" :
            if self.bDataFg > 0  and self.strDataName[0] != " " :
                if self.strDataName == "length" :
                    if find(contents, ",") == -1 :
                        self.dict.setValue("length", int(contents))
                        self.dict.setValue("dotLen", 0)
                    else :
                        dupLen = split(contents, ",")
                        self.dict.setValue("length", int(dupLen[0]))
                        self.dict.setValue("dotLen", int(dupLen[1]))
                else :
                    self.dict.setValue(self.strDataName, contents)
